package com.lei.mvp_day02_library.net;

import com.lei.mvp_day02_library.net.baseurl.ConfigUrl;

import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetWorkHttp {
    private static NetWorkHttp instance = new NetWorkHttp();
    private static Retrofit retrofit;
    private static NetWorkHttp getInstance(){
        retrofit = new Retrofit.Builder()
                .baseUrl(ConfigUrl.BASEURL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(createrHttp())
                .build();
        return instance;
    }

    private static OkHttpClient createrHttp() {
        OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(createrLoggin())
                .writeTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .build();
        return client;
    }

    private static Interceptor createrLoggin() {
        return new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC);
    }
    public <T> T create(Class<T> service){
        return retrofit.create(service);
    }
}
